package com.matrix.yukun.matrix.main_module.wallpaper.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.matrix.yukun.matrix.BaseActivity;
import com.matrix.yukun.matrix.R;
import com.matrix.yukun.matrix.main_module.entity.PhoneCategoryInfo;
import com.matrix.yukun.matrix.main_module.netutils.NetworkUtils;
import com.matrix.yukun.matrix.main_module.wallpaper.adapter.RVWallpaperCategoryAdapter;
import com.matrix.yukun.matrix.tool_module.garrary.activity.PhotoListActivity;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class WallpaperCategoryActivity extends BaseActivity {

    private static int mType;
    public static int PHONE = 1;
    public static int COMPUTER = 2;
    @BindView(R.id.iv_setting)
    ImageView ivBack;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    //    @BindView(R.id.sm_refresh)
//    SmartRefreshLayout smRefresh;
    private String phoneUrl = "http://service.picasso.adesk.com/v1/vertical/category?";
    private String computerUrl = "http://service.picasso.adesk.com/v1/wallpaper/category";
    private List<PhoneCategoryInfo> mCategoryInfos = new ArrayList<>();
    private RVWallpaperCategoryAdapter mAdapter;

    public static void start(int type, Context context) {
        mType = type;
        Intent intent = new Intent(context, WallpaperCategoryActivity.class);
        context.startActivity(intent);
    }

    @Override
    public int getLayout() {
        return R.layout.activity_phone_wallpaper;
    }

    @Override
    public void initView() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        recyclerview.setLayoutManager(gridLayoutManager);
        mAdapter = new RVWallpaperCategoryAdapter(R.layout.wallpaper_category_layout, mCategoryInfos);
        recyclerview.setAdapter(mAdapter);
    }

    @Override
    public void initListener() {
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            if(mType==PHONE){
                PhoneWallpaperListActivity.start(mCategoryInfos.get(position).getId(),WallpaperCategoryActivity.this);
            }else {
                ComputerWallpaperListActivity.start(mCategoryInfos.get(position).getId(),WallpaperCategoryActivity.this);
            }
        });
    }

    @Override
    public void initDate() {
        if (mType == PHONE) {
            tvTitle.setText("手机壁纸分类");
            NetworkUtils.networkGet(phoneUrl)
                    .addParams("adult", "false")
                    .addParams("first", "1").build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {

                        }

                        @Override
                        public void onResponse(String response, int id) {
                            JSONObject jsonObject = null;
                            try {
                                jsonObject = new JSONObject(response);
                                String msg = jsonObject.optString("msg");
                                if (msg.equals("success")) {
                                    JSONObject res = jsonObject.optJSONObject("res");
                                    Gson gson = new Gson();
                                    List<PhoneCategoryInfo> vertical = gson.fromJson(res.optJSONArray("category").toString(), new TypeToken<List<PhoneCategoryInfo>>() {
                                    }.getType());
                                    if (vertical.size() > 0) {
                                        mCategoryInfos.addAll(vertical);
                                        mAdapter.setNewData(mCategoryInfos);
                                    }
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    });
        } else if (mType == COMPUTER) {
            tvTitle.setText("电脑壁纸分类");
            NetworkUtils.networkGet(computerUrl)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {

                        }

                        @Override
                        public void onResponse(String response, int id) {
                            JSONObject jsonObject = null;
                            try {
                                jsonObject = new JSONObject(response);
                                String msg = jsonObject.optString("msg");
                                if (msg.equals("success")) {
                                    JSONObject res = jsonObject.optJSONObject("res");
                                    Gson gson = new Gson();
                                    List<PhoneCategoryInfo> vertical = gson.fromJson(res.optJSONArray("category").toString(), new TypeToken<List<PhoneCategoryInfo>>() {
                                    }.getType());
                                    if (vertical.size() > 0) {
                                        mCategoryInfos.addAll(vertical);
                                        mAdapter.setNewData(mCategoryInfos);
                                    }
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    });
        }
    }


    @OnClick(R.id.iv_setting)
    public void onViewClicked() {
        finish();
    }
}
